package com.vacomall.erp.web;

import com.vacomall.erp.dao.ImagesMapper;
import com.vacomall.erp.service.IQuarantineCertificateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by root1 on 16/8/25.
 */

@Controller
public class UploadAction {

    final static Logger log = LoggerFactory.getLogger(UploadAction.class);

    @Autowired
    private IQuarantineCertificateService quarantineCertificateService;

    /*这儿填写你存放图片的文件夹全地址*/
    private final String IMAGES = "/Users/root1/IdeaProjects/vacomall_certificate/src/main/webapp/resource/imgs/";

    /*这儿填写你存放要缩小图片的文件夹全地址*/
    String  OUTPUTFOLDER = "/Users/root1/Downloads/h5/dd/";

    /**
     * 上传检疫证书
     * @param file
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "imgs" ,method = RequestMethod.POST)
    @ResponseBody
    public Map upload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, ModelMap model) {

        String fileName = file.getOriginalFilename();
        File targetFile = new File(IMAGES, fileName);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        int results = 0;
        //保存
        try {
            file.transferTo(targetFile);
            //把图片地址传到方法里面生成压缩图  保存到数据库
            results  = quarantineCertificateService.addThumbnailImage(IMAGES + fileName,OUTPUTFOLDER);
            Map<String, Object> map = new HashMap<>();
            map.put("statusCode","200");
            map.put("message","上传成功！");
            map.put( "filename","http://localhost:8080/downloadAction/getImage?imageId="+results);
            map.put("imagsId", results);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
